package com.vicene.gmail.ware.listener;

import com.alibaba.fastjson.TypeReference;
import com.rabbitmq.client.Channel;
import com.vicene.gmail.common.mq.OrderTo;
import com.vicene.gmail.common.mq.StockDetailTo;
import com.vicene.gmail.common.mq.StockLockedTo;
import com.vicene.gmail.common.utils.R;
import com.vicene.gmail.ware.dao.WareSkuDao;
import com.vicene.gmail.ware.entity.WareOrderTaskDetailEntity;
import com.vicene.gmail.ware.entity.WareOrderTaskEntity;
import com.vicene.gmail.ware.feign.OrderFeignService;
import com.vicene.gmail.ware.feign.ProductFeignService;
import com.vicene.gmail.ware.service.WareOrderTaskDetailService;
import com.vicene.gmail.ware.service.WareOrderTaskService;
import com.vicene.gmail.ware.service.WareSkuService;
import com.vicene.gmail.ware.vo.OrderVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
@RabbitListener(queues = {"stock.release.stock.queue"})
public class StockReleaseListener {

    @Autowired
    OrderFeignService orderFeignService;

    @Autowired
    ProductFeignService productFeignService;

    @Autowired
    private WareOrderTaskService wareOrderTaskService;

    @Autowired
    private WareOrderTaskDetailService wareOrderTaskDetailService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    WareSkuService wareSkuService;

    @RabbitHandler
    public void handleStockLockedRelease(StockLockedTo to , Message message , Channel channel) throws IOException {
        System.out.printf("收到解锁库存消息");
        try{
            wareSkuService.unLockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag() , false);

        }catch(Exception e){
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag() , true);
        }
    }


    @RabbitHandler
    public void handleOrderCloseRelease(OrderTo orderTo , Message message , Channel channel) throws IOException {
        System.out.printf("订单关闭，准备解锁库存");
        try{
            wareSkuService.unLockStock(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag() , false);

        }catch(Exception e){
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag() , true);
        }
    }

}
